load("//bazel:macros.bzl", "bool_flag", "string_flag_with_values")

licenses(["notice"])

# @platforms is found at https://github.com/bazelbuild/platforms
package(default_visibility = ["//:__subpackages__"])

config_setting(
    name = "linux_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:linux",
    ],
)

# Apple devices with intel processors released before the shift to the M1 chip
# will use this config setting. Because M1 chips use the arm cpu instruction set,
# this flag will not be used.
config_setting(
    name = "mac_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:macos",
    ],
)

config_setting(
    name = "mac_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        "@platforms//os:macos",
    ],
)

config_setting(
    name = "windows_x64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:windows",
    ],
)

config_setting(
    name = "linux_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        "@platforms//os:linux",
    ],
)

config_setting(
    name = "debug_build",
    values = {"compilation_mode": "dbg"},
)

config_setting(
    name = "fast_build",
    values = {"compilation_mode": "fastbuild"},
)

config_setting(
    name = "release_build",
    values = {"compilation_mode": "opt"},
)

constraint_value(
    name = "fuchsia",
    constraint_setting = "@platforms//os:os",
)

config_setting(
    name = "fuchsia_arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        ":fuchsia",
    ],
)

# We define this here because the emscripten toolchain calls the cpu wasm, whereas the
# bazelbuild/platforms call it wasm32. https://github.com/emscripten-core/emsdk/issues/919
config_setting(
    name = "cpu_wasm",
    values = {
        "cpu": "wasm",
    },
)

# =============================================================================
#                     Configurable Skia Features
# =============================================================================
# These are flags that we can specify when invoking bazel build to turn on and
# off certain features, such as GPU backend, or codec support.
# https://bazel.build/rules/config#defining-build-settings
# For example, to enable harfbuzz and icu, one would run
# bazel build //:skia-core --//bazel/common_config_settings:use_harfbuzz \
#             --//bazel/common_config_settings:use_icu
# This is a bit wordy, so we define aliases in the //.bazelrc file that condense this to
# bazel build //:skia-core --with_harfbuzz --with_icu
#
# Developers can specify their own short-hands by making a .bazelrc file in their home
# directory. https://bazel.build/docs/bazelrc#bazelrc-file-locations
#
# We check in some Bazel configs for "blessed builds" in //bazel/buildrc.
#
# We can also define flags closer to where they have the most impact. For example
# //src/pdf:enable_pdf_backend.

string_flag_with_values(
    default = "empty_fontmgr_factory",
    flag_name = "fontmgr_factory",
    values = [
        # Makes the default SkFontMgr load fonts from a hard-coded directory on disk.
        "custom_directory_fontmgr_factory",
        # Makes the default SkFontMgr load fonts from an SkEmbeddedResource that has been compiled
        # into the binary, e.g. with //tools/embed_resources.py
        "custom_embedded_fontmgr_factory",
        # Makes the default SkFontMgr return empty fonts (e.g. SkTypeface_Empty). This is typically
        # used when someone wants to make their own custom SkFontMgr objects, but does not want the
        # default SkFontMgr to do anything (e.g. force usage of the custom one).
        "custom_empty_fontmgr_factory",
        # Makes the default SkFontMgr return null. Typically used when font support is not desired.
        "empty_fontmgr_factory",
        "fontconfig_fontmgr_factory",
    ],
)

# These flags need only be set if additional functionality beyond the fontmgr_factory flag is
# required. For example, the setting fontmgr_factory to custom_embedded_fontmgr_factory does not
# require setting include_fontmgr to custom_embedded_fontmgr, because those sources and settings
# will already be compiled in due to the _factory flag.
string_flag_with_values(
    flag_name = "include_fontmgr",
    multiple = True,
    values = [
        # Allows the construction of an SkFontMgr that loads files from a programmatically
        # defined directory on disk.
        "custom_directory_fontmgr",
        # Allows the construction of an SkFontMgr which can load fonts from an SkEmbeddedResource
        # or from another source of raw bytes.
        "custom_embedded_fontmgr",
        # Allows the construction of an SkFontMgr which returns empty fonts.
        "custom_empty_fontmgr",
        "fontconfig_fontmgr",
    ],
)

string_flag_with_values(
    flag_name = "include_decoder",
    multiple = True,
    values = [
        "gif_decode_codec",
        "jpeg_decode_codec",
        "png_decode_codec",
        "raw_decode_codec",
        "webp_decode_codec",
    ],
)

string_flag_with_values(
    flag_name = "include_encoder",
    multiple = True,
    values = [
        "jpeg_encode_codec",
        "png_encode_codec",
        "webp_encode_codec",
    ],
)

bool_flag(
    default = True,
    flag_name = "enable_effect_serialization",
)

bool_flag(
    # See SkTraceEventCommon.h for more on this type of tracing.
    default = True,
    flag_name = "enable_tracing",
)

bool_flag(
    default = False,
    flag_name = "use_harfbuzz",
)

bool_flag(
    default = False,
    flag_name = "use_icu",
)
